/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.can.bc;

import edu.upn.sigemfi.can.be.TransaccionExternaWeb;
import edu.upn.sigemfi.can.be.TransaccionGiroWeb;
import edu.upn.sigemfi.can.be.TransaccionInternaWeb;
import edu.upn.sigemfi.can.be.TransaccionServicioWeb;
import edu.upn.sigemfi.can.be.TransaccionWeb;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Orlando ( U )
 */
public class TransaccionWebDelegate {

    private TransaccionWebFacadeLocal local;

    public TransaccionWebDelegate(){
        local = lookupTransaccionWebFacade();
    }

    //Transaccion Interna Web
    public void crearTransaccionInterna(TransaccionInternaWeb transaccionInternaWeb) throws Exception{
        try {
            local.create(transaccionInternaWeb);
        } catch (Exception ex) {
            local=lookupTransaccionWebFacade();
            throw new Exception("Crear Transaccion Interna Web"+ex.getMessage());
        }
    }
    //Transaccion Externa Web

    public void crearTransaccionExterna(TransaccionExternaWeb transaccionExternaWeb) throws Exception{
        try {
            local.create(transaccionExternaWeb);
        } catch (Exception ex) {
            local=lookupTransaccionWebFacade();
            throw new Exception("Crear Transaccion Externa Web"+ex.getMessage());
        }
    }
    //Transaccion Pago de Servicio

   public void crearTransaccionServicio(TransaccionServicioWeb transaccionServicioWeb) throws Exception{
       try {
           local.create(transaccionServicioWeb);
       } catch (Exception ex) {
           local=lookupTransaccionWebFacade();
           throw new Exception("Crear el Pago del Servicio"+ex.getMessage());
       }
   }

    //Transaccion Giro WEb

    public List<TransaccionGiroWeb> listarGiroWebPorDestinatario(String criterio) throws Exception
    {
        try{
            return local.findGiroWebByDestinatario(criterio);
        }catch (Exception ex)
        {
            local=lookupTransaccionWebFacade();
            throw new Exception("Listar Giros por Destinatario"+ex.getMessage());
        }
    }

    public void crearGiroWeb(TransaccionGiroWeb transaccionGiroWeb) throws Exception
    {
        try{
            local.create(transaccionGiroWeb);
        }
        catch(Exception ex){
            local=lookupTransaccionWebFacade();
            throw new Exception("Crear Giro Web"+ex.getMessage());
        }
    }

     public void editarGiroWeb(TransaccionGiroWeb transaccionGiroWeb) throws Exception
    {
        try{
            local.edit(transaccionGiroWeb);
        }
        catch(Exception ex){
            local=lookupTransaccionWebFacade();
            throw new Exception("Editar Giro Web"+ex.getMessage());
        }
    }

       public void editarExternaWeb(TransaccionExternaWeb transaccionExternaWeb) throws Exception
    {
        try{
            local.edit(transaccionExternaWeb);
        }
        catch(Exception ex){
            local=lookupTransaccionWebFacade();
            throw new Exception("Editar Transaccion Externa Web"+ex.getMessage());
        }
    }
       public void editarInternaWeb(TransaccionInternaWeb transaccionInternaWeb) throws Exception
    {
        try{
            local.edit(transaccionInternaWeb);
        }
        catch(Exception ex){
            local=lookupTransaccionWebFacade();
            throw new Exception("Editar Transaccion Interna Web"+ex.getMessage());
        }
    }
    public TransaccionGiroWeb buscarGiroWeb (Long codigo) throws Exception {
        try {
            return local.findGiroWeb(codigo);
        } catch (Exception e) {
            local = lookupTransaccionWebFacade();
            throw new Exception("Buscar Giro Web. " + e.getMessage(), e);
        }
    }

     public TransaccionExternaWeb buscarExternaWeb (Long codigo) throws Exception {
        try {
            return local.findExternaWeb(codigo);
        } catch (Exception e) {
            local = lookupTransaccionWebFacade();
            throw new Exception("Buscar Externa Web. " + e.getMessage(), e);
        }
    }
     public TransaccionInternaWeb buscarInternaWeb (Long codigo) throws Exception {
        try {
            return local.findInternaWeb(codigo);
        } catch (Exception e) {
            local = lookupTransaccionWebFacade();
            throw new Exception("Buscar Interna Web. " + e.getMessage(), e);
        }
    }
    public List<TransaccionGiroWeb> listarGiroWeb() throws Exception {
        try {
            return local.findAllGirosWebs();
        } catch (Exception e) {
            local = lookupTransaccionWebFacade();
            throw new Exception("Listar Giros Web. " + e.getMessage(), e);
        }
    }

    public List<TransaccionWeb> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupTransaccionWebFacade();
            throw new Exception("Listar Transacciones. " + e.getMessage(), e);
        }
    }

    private TransaccionWebFacadeLocal lookupTransaccionWebFacade() {
        try {
            Context c = new InitialContext();
            return (TransaccionWebFacadeLocal) c.lookup("java:comp/env/TransaccionWebFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }




}


